#include "obstacles.h"

obstacles::obstacles(int a)
{
    index=a;
    switch(a){
    case 1:
        pix.load(":/res/building2.png");
        pix=pix.scaled(pix.width()*1.1,pix.height()*1.5);
        o_x=700+pix.width();
        o_y=350-pix.height()+15+15;
        break;
    case 2:
        pix.load(":/res/tree.png");
        pix=pix.scaled(pix.width(),pix.height()*1.1);
        o_x=700+pix.width();
        o_y=350-pix.height()+15;
        break;
    case 3:
        pix.load(":/res/building3.png");
        pix=pix.scaled(pix.width()*1.1,pix.height()*1.3);
        o_x=700+pix.width();
        o_y=350-pix.height()+8+15;
        break;
    case 4:
        pix.load(":/res/cloud.png");
        pix=pix.scaled(pix.width()*0.85,pix.height()*0.85);
        o_x=700+pix.width();
        o_y=75+15;
        break;
    case 5:
        pix.load(":/res/bird.png");
        pix=pix.scaled(pix.width()*0.8,pix.height()*0.8);
        o_x=700+pix.width();
        o_y=60+15;
        break;
    case 6:
        pix.load(":/res/heli3.png");
        pix=pix.scaled(pix.width()*0.4,pix.height()*0.4);
        o_x=700+pix.width();
        o_y=13+15;
        break;

    }

    is_free3=true;

    o_speed=2;
    if(index>4)o_speed=3;

    o_rect.setWidth(pix.width()-20);
    o_rect.setHeight(pix.height()-20);
    o_rect.moveTo(o_x+10,o_y+10);

}

void obstacles::updatePosition()
{
    if(is_free3)return;

    //上下移动
    if(index==5){
        o_y+=offset;
        if(o_y<33+15||o_y>95+15)offset=-offset;
    }
    if(index==6){
        o_y+=offset;
        if(o_y<2+15||o_y>25+15)offset=-offset;
    }

    o_x-=o_speed;

    o_rect.moveTo(o_x+15,o_y+15);

    //超出屏幕，重置空闲状态
    if(o_x<-pix.width()){
        is_free3=true;
    }

}
